package com.example.demo.controller;

import com.example.demo.entity.Article;
import com.example.demo.service.web.ArticleWebService;
import com.example.demo.service.web.LabelWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * RouteController class
 * @author yjq
 */
@Controller
public class RouteController {

    @Autowired
    ArticleWebService articleService;
    @Autowired
    LabelWebService labelService;

    /**
     * 默认博客首页为网站首页
     *
     */
    @RequestMapping("/")
    public String blog(Model model) {
        List<Article> list = articleService.selectAll();
        List<Article> type1 = new ArrayList<>();
        List<Article> type2 = new ArrayList<>();
        List<Article> type3 = new ArrayList<>();
        List<Article> type4 = new ArrayList<>();
        for (Article article : list) {
            switch (article.getArticleType()) {
                case "优秀文章":
                    type1.add(article);
                    break;
                case "倾听世界":
                    type2.add(article);
                    break;
                case "叫醒耳朵":
                    type3.add(article);
                    break;
                case "每日一笑":
                    type4.add(article);
                    break;
                default:
                    break;
            }
        }
        model.addAttribute("label",labelService.selectLabel());
        model.addAttribute("type1", type1.size() > 3 ? type1.subList(0, 2) : type1);
        model.addAttribute("type2", type2.size() > 10 ? type2.subList(0, 9) : type2);
        model.addAttribute("type3", type3.size() > 12 ? type3.subList(0, 11) : type3);
        model.addAttribute("type4", type4.size() > 2 ? type4.subList(0, 1) : type4);
        model.addAttribute("articleTop7", articleService.selectTop7());
        model.addAttribute("title","Super Star");
        return "web_index";
    }

}
